import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.JButton;

/**
 * Fenêtre de consultation d'un ouvrage
 * Code du JFrame généré par Window Builder/Swing Designer.
 * @author IUT,  J.Berthier-Delacour  
 * @version 1.0
 */

public class VueSaisieParution extends Vue{
	
	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private JTextField textFieldIssn;
	private JTextField textFieldIdentifiant;
	private JTextField textFieldNbArticles;
	private JButton btnRechercher;
	private JButton btnVerifier;
	private JButton btnOk;
	private String issn;
	private int nbArticles;
	
	public VueSaisieParution(Controleur controleur)
	{
		super(controleur);
		setTitle("Enregistrement d'une nouvelle parution");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblIssn = new JLabel("ISSN");
		lblIssn.setBounds(89, 32, 68, 15);
		contentPane.add(lblIssn);
		
		textFieldIssn = new JTextField();
		textFieldIssn.setBounds(175, 30, 114, 19);
		contentPane.add(textFieldIssn);
		textFieldIssn.setColumns(10);
		
		JLabel lblIdentifiant = new JLabel("identifiant");
		lblIdentifiant.setBounds(89, 81, 74, 15);
		contentPane.add(lblIdentifiant);
		
		textFieldIdentifiant = new JTextField();
		textFieldIdentifiant.setBounds(175, 79, 114, 19);
		textFieldIdentifiant.setEnabled(false);
		contentPane.add(textFieldIdentifiant);
		textFieldIdentifiant.setColumns(10);
		
		btnRechercher = new JButton("Rechercher");
		btnRechercher.addActionListener(new ActionListener()  {
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				issn = textFieldIssn.getText();
				if (getControleur().existeIssn(issn) )  {
					textFieldIdentifiant.setVisible(true);
					btnVerifier.setVisible(true);
					textFieldIssn.setVisible(false);
					btnRechercher.setVisible(false);
				}
				else  {
					Message dialog = new Message("Périodique non existant");
					dialog.setVisible(true);
				}
			}
		} );
		btnRechercher.setBounds(323, 27, 107, 25);
		contentPane.add(btnRechercher);
		
		btnVerifier = new JButton("Vérifier");
		btnVerifier.addActionListener(new ActionListener()  {
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				String identifiant = textFieldIdentifiant.getText();
				if (!getControleur().existeParution(issn, identifiant))  {
					textFieldIdentifiant.setVisible(false);
					btnVerifier.setVisible(false);
					textFieldNbArticles.setVisible(true);
					btnOk.setVisible(true);
				}
			}
		} );
		btnVerifier.setBounds(323, 76, 107, 25);
		btnVerifier.setEnabled(false);
		contentPane.add(btnVerifier);
		
		JLabel lblNbArticles = new JLabel("Nb articles");
		lblNbArticles.setBounds(89, 146, 74, 15);
		contentPane.add(lblNbArticles);
		
		textFieldNbArticles = new JTextField();
		textFieldNbArticles.setBounds(175, 144, 114, 19);
		textFieldNbArticles.setEnabled(false);
		contentPane.add(textFieldNbArticles);
		textFieldNbArticles.setColumns(10);
		
		btnOk = new JButton("Ok");
		btnOk.addActionListener(new ActionListener()  {
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				nbArticles = Integer.parseInt(textFieldNbArticles.getText());
				for (int i = 0; i < nbArticles; i ++)  {
					getControleur().saisirArticle();
				}
			}
		});
		btnOk.setBounds(323, 141, 107, 25);
		btnOk.setEnabled(false);
		contentPane.add(btnOk);
		
		
	}
}
